package com.ithiema.write;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.ithiema.pojo.Employee;
import com.ithiema.utils.TestFileUtil;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
    进阶写数据
 */
public class ManyWrite {

    // 批量写数据
    @Test
    public void testWrite(){
        String name = TestFileUtil.getPath() + "批量写数据" + System.currentTimeMillis() + ".xlsx";
        // 与文件想关联的写对象
        try (ExcelWriter writer = EasyExcel.write(name, Employee.class).build()) {
            // 准备一个表对象
            WriteSheet sheet = EasyExcel.writerSheet("批量写练习").build();
            // 利用writer向sheet表写数据
            for (int i = 1; i <= 3; i++) {
                writer.write(data(10000), sheet);
            }
        }
    }

    // 准备测试数据的方法
    private List<Employee> data(int i) {
        List<Employee> list = new ArrayList<>();
        for (int j = 1; j <= i; j++) {
            list.add(new Employee(j,"测试数据"+j,6.6*j,new Date()));
        }
        return list;
    }
}
